package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dto.UserSearchDto;
import com.usian.model.search.pojo.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.ApBehaviorEntryFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private ApBehaviorEntryFeign apBehaviorEntryFeign;

    @Override
    public ResponseResult findUserSearch(UserSearchDto userSearchDto) {

        if(userSearchDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtils.getUser();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        ApBehaviorEntry behaviorEntry = apBehaviorEntryFeign.findByUserIdOrEntryId(user.getId(), userSearchDto.getEquipmentId());
        if(behaviorEntry==null)
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户行为实体不存在");
        }


        IPage pageParam = new Page(userSearchDto.getPageNum(), userSearchDto.getPageSize());
        List<ApUserSearch> searchList = this.list(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getStatus, 1));
        IPage result =page(pageParam,Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getStatus, 1)
        .eq(ApUserSearch::getEntryId,behaviorEntry.getEntryId()));

        ResponseResult responseResult=new PageResponseResult(userSearchDto.getPageNum(),userSearchDto.getPageSize(),
                (int)result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    @Async("taskExecutor")
    public void insert(Integer entryId, String searchWords) {

        //线程池异步保存数据
        log.info("=============================异步请求");
        ApUserSearch apUserSearch = this.getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId).eq(ApUserSearch::getKeyword, searchWords));

        if(apUserSearch!=null && apUserSearch.getStatus()==1){
            log.info("搜索记录已存在");
        }else if (apUserSearch!=null && apUserSearch.getStatus()==0){
            apUserSearch.setStatus(1);
            this.updateById(apUserSearch);
        }else{
            ApUserSearch apUserSearch1 = new ApUserSearch();
            apUserSearch1.setEntryId(entryId);
            apUserSearch1.setStatus(1);
            apUserSearch1.setKeyword(searchWords);
            apUserSearch1.setCreatedTime(new Date());
            this.save(apUserSearch1);
        }
    }
}
